package com.google.project.server;

import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.project.client.Service.SubConstructorService;
import com.google.project.server.entity.Project;
import com.google.project.server.entity.SubConstructor;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.SubConstructorDTO;

@SuppressWarnings({ "serial", "unused" })
public class SubConstructorImpl extends RemoteServiceServlet implements SubConstructorService{

	@Override
	public SubConstructorDTO createSubConstructor(
			SubConstructorDTO subConstructorDTO ,String projectId) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		SubConstructor subConstrustor = null;
		if (subConstructorDTO.getConstructorId()!=null &&  !subConstructorDTO.getConstructorId().equalsIgnoreCase("")) {
			updateSubConstructor(subConstructorDTO);

		} else {
			try {
				 Project project =   pm.getObjectById(Project.class, projectId);
				subConstrustor = new SubConstructor(subConstructorDTO);
				project.getListOfSubContructor().add(subConstrustor);
				pm.makePersistent(project);
			} finally {
				pm.close();
			}
		}
		return subConstructorDTO;
	}


	@Override
	public SubConstructorDTO getSubConstructorById(String subConstructorId) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    SubConstructor subCTInfo;
	    try {
	    	subCTInfo = pm.getObjectById(SubConstructor.class, subConstructorId);
	
	    } finally {
	      pm.close();
	    }

	    return subCTInfo.toDTO();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SubConstructorDTO> getSubConstructorList() {
		// TODO Auto-generated method stub
		List<SubConstructorDTO> subConstructorList = new ArrayList<SubConstructorDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(SubConstructor.class);
		try {
			List<SubConstructor> list = (List<SubConstructor>) q.execute();
			for (SubConstructor subConstructor : list) {
				subConstructorList.add(subConstructor.toDTO());
			}
		} catch (Exception e) {
			// logger.warning(e.getMessage());
		} finally {
			pm.close();
		}
		return subConstructorList;
	}

	@Override
	public List<SubConstructorDTO> searchSubConstructorList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateSubConstructor(SubConstructorDTO subConstructorDTO) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		SubConstructor subCT = null;
		try {
			subCT = pm
					.getObjectById(SubConstructor.class, subConstructorDTO.getConstructorId());
			subCT.updateFromDTO(subConstructorDTO);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}


	@Override
	public void deleteSubConList(List<SubConstructorDTO> list) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			for (SubConstructorDTO subConstructorDTO : list) {
				SubConstructor subConstructor = pm.getObjectById(SubConstructor.class, subConstructorDTO.getConstructorId());
				if (subConstructor != null) {
					pm.deletePersistent(subConstructor);
				}
			}
			
		} finally {
			pm.close();
		}
		
	}

}
